package com.test.travelinformation.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.test.travelinformation.R;
import com.test.travelinformation.entity.Travel;
import com.test.travelinformation.utils.UpImage;

import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CircleAdapter extends BaseQuickAdapter<Travel, BaseViewHolder> {
    Context ccontext;
    UpImageAdapter adapte;

    public CircleAdapter(Context context, List<Travel> data) {
        super(R.layout.item_circle, data);
        ccontext = context;
    }

    @Override
    protected void convert(@NotNull BaseViewHolder baseViewHolder, Travel travel) {
        CardView cardView = baseViewHolder.getView(R.id.cv_1);

        switch (baseViewHolder.getPosition() % 3) {
            case 2:
                cardView.setCardBackgroundColor(Color.parseColor("#56E5B4"));
                break;
            case 1:
                cardView.setCardBackgroundColor(Color.parseColor("#A7F38E"));
                break;
            default:
                cardView.setCardBackgroundColor(Color.parseColor("#F9F871"));
                break;
        }

        TextView textView = baseViewHolder.getView(R.id.tv_1);
        textView.setText(travel.getDescribe());
        TextView textView2 = baseViewHolder.getView(R.id.tv_2);
        textView2.setText(travel.getUsername() + "：");
        List<String> data = new ArrayList<>();

        if (travel.getImages() != null) {
            Gson gson = new Gson();
            List<UpImage> persons = gson.fromJson(travel.getImages(), new TypeToken<List<UpImage>>() {
            }.getType());
            if (persons != null) {
                for (int i = 0; i < persons.size(); i++) {
                    data.add(persons.get(i).getImgurl());
                }
            }
        }
        adapte = new UpImageAdapter(ccontext, data);
        RecyclerView recyclerView = baseViewHolder.getView(R.id.rec_1);
        recyclerView.setLayoutManager(new GridLayoutManager(ccontext, 3));
        recyclerView.setAdapter(adapte);

    }

}
